<?php

namespace app\models;

use Yii;

/**
 * This is the model class with sharing functions
 *
 */
class Helpers
{
    static public function checkExist($type, $id = NULL)
    {
        $id = ($id == NULL) ? Yii::$app->session->get('pt_id') : $id;
        $name = "upload/images/{$type}/".$id.".jpg";
		if (is_readable("../web/".$name)) {
		  	$name = 'src="../../'.$name.'?id='.rand(1, 100).'"';
		  	$href = "/media/view?id=".$id.".jpg&type={$type}";
		} else {
		  	$name = '';
		  	$href = '#';
		}

		return ['name' => $name, 'href' => $href];
    }

    /**
     * trim string like "++1++2+3+4+" to "1+2+3+4".
     * @param string $string
     * @param boolean $return_array
     * @return trimmed string
     */
    static public function clearStringForArray($string, $return_array = false)
    {
        $labels = explode("+", $string);
        foreach ($labels as $key => $label) {
            if ($label == '') unset($labels[$key]);
        }
        sort($labels);
        return $return_array ? $labels : implode("+", $labels);
    }

    static public function getEnumValues($table, $column)
    {
        $column = (new \yii\db\Query())
            ->select(['SUBSTRING(COLUMN_TYPE,5) as enums'])
            ->from('information_schema.COLUMNS')
            ->where(['TABLE_SCHEMA' => 'hospital'])
            ->where(['TABLE_NAME' => $table])
            ->where(['COLUMN_NAME' => $column])
            ->one();

        if (!empty($column) && !empty($column['enums'])) {
            dd($column['enums']);
        }

        return null;
    }
}
